import {PluginCommand} from "./PluginCommand";
import {parseString} from 'xml2js';

class ElectricalCableCommand extends PluginCommand {
    /**
     * 构造函数
     */
    constructor() {
        super();
    }

    async createElectricalCable(params) {
        if(this.toolQuery){
            let res = await this.toolQuery.createElectricalCable(params);
            return res;
        }
    }

    async readXmlFile(file){
        return new Promise(async resolve => {
            let reader = new FileReader();
            reader.readAsText(file.raw);
            let fileString = null;
            reader.onload = (e) => {
                fileString = e.target.result;
                resolve(fileString);
            }
        })
    }
    xmlStringAnalysis(xmlString){

        let xmlInfo = [];

        let parser = new DOMParser();
        let xmlDoc = parser.parseFromString(xmlString,"application/xml");
        //所有线路
        let routesInfo = xmlDoc.getElementsByTagName("CableInfo");
        if(!routesInfo || routesInfo.length == 0){
            return;
        }
        let routeList = routesInfo[0].getElementsByTagName("Route");
        if(!routeList || routeList.length == 0){
            return;
        }

        //遍历所有的线路
        for(let routeIndex = 0;routeIndex < routeList.length;routeIndex++){
            let routeInfo = {};
            let routeNode = routeList[routeIndex];
            if(!routeNode){
                continue;
            }
            let routeName = routeNode.getAttribute("RouteName");
            routeInfo.routeName = routeName;

            let cableList = routeNode.getElementsByTagName("Cable");
            if(!cableList){
                continue;
            }

            routeInfo.cablesInfo = [];
            for(let cableIndex = 0;cableIndex < cableList.length;cableIndex++){
                let cableInfo = {};
                let cableNode = cableList[cableIndex];
                if(!cableNode){
                    continue;
                }
                let cableName = cableNode.getAttribute("Name");
                cableInfo.cableName = cableName;

                let pointsNode = cableNode.getElementsByTagName("points");
                if(!pointsNode || pointsNode.length == 0){
                    continue;
                }
                let pointList = pointsNode[0].getElementsByTagName("point");
                if(!pointList || pointList.length == 0){
                    continue;
                }

                let specificationInfoList = cableNode.getElementsByTagName("SpecificationInfo");
                if(!specificationInfoList || !specificationInfoList[0]){
                    continue;
                }

                let specification = specificationInfoList[0];
                let startDir = this.getNodeValue(cableNode,"startDir");
                let endDir = this.getNodeValue(cableNode,"endDir");
                cableInfo.startDir = this.getCoordinate(startDir);
                cableInfo.endDir = this.getCoordinate(endDir);

                let pointsInfo = [];
                //获取离散点
                for(let pointIndex = 0;pointIndex < pointList.length;pointIndex++){
                    let pointNode = pointList[pointIndex];
                    if(!pointNode){
                        continue;
                    }

                    let point = pointNode.textContent;
                    pointsInfo.push(this.getCoordinate(point));
                }
                cableInfo.points = pointsInfo;

                let diameter = this.getNodeValue(specification,"diameter");
                let minBendRadius = this.getNodeValue(specification,"minBendRadius");
                let unitLengthSize = this.getNodeValue(specification,"unitLengthSize");
                let color = this.getNodeValue(specification,"color");
                let colorNum = this.getNodeValue(specification,"colorNum");
                let introduce = this.getNodeValue(specification,"introduce");
                let wireNum = this.getNodeValue(specification,"wireNum");

                let specificationInfo = {};
                specificationInfo.diameter = diameter;
                specificationInfo.minBendRadius = minBendRadius;
                specificationInfo.unitLengthSize = unitLengthSize;
                specificationInfo.color = color;
                specificationInfo.colorNum = colorNum;
                specificationInfo.introduce = introduce;
                specificationInfo.wireNum = wireNum;

                cableInfo.insertDoc = {id:"67eb4162aebc8f201bd77822",
                  instanceName:"零件 1"};

                let wireCores = cableNode.getElementsByTagName("WireCores");
                if(wireCores){
                    let wireCoreSpecificationInfoList = cableNode.getElementsByTagName("WireCoreSpecificationInfo");
                    let wireCoresInfo = [];
                    if(wireCoreSpecificationInfoList){
                        for(let wireCoreIndex = 0;wireCoreIndex < wireCoreSpecificationInfoList.length;wireCoreIndex++){
                            let wireCore = wireCoreSpecificationInfoList[wireCoreIndex];
                            if(!wireCore){
                                continue;
                            }
                            let wireCoreInfo = {};
                            let wireCoreName = wireCore.getAttribute("Name");
                            let wireDiameter = this.getNodeValue(wireCore,"diameter");
                            let wireMinBendRadius = this.getNodeValue(wireCore,"minBendRadius");
                            let wireUnitLengthSize = this.getNodeValue(wireCore,"unitLengthSize");
                            let wireColor = this.getNodeValue(wireCore,"color");
                            let wireColorNum = this.getNodeValue(wireCore,"colorNum");
                            let wireIntroduce = this.getNodeValue(wireCore,"introduce");

                            wireCoreInfo.name = wireCoreName;
                            wireCoreInfo.diameter = wireDiameter;
                            wireCoreInfo.minBendRadius = wireMinBendRadius;
                            wireCoreInfo.unitLengthSize = wireUnitLengthSize;
                            wireCoreInfo.color = wireColor;
                            wireCoreInfo.colorNum = wireColorNum;
                            wireCoreInfo.introduce = wireIntroduce;
                            wireCoresInfo.push(wireCoreInfo);
                        }
                    }
                    specificationInfo.wireCoresInfo = wireCoresInfo;
                }

                cableInfo.specificationInfo = specificationInfo;
                routeInfo.cablesInfo.push(cableInfo);
            }
            xmlInfo.push(routeInfo);
        }

        return xmlInfo;
    }

    //获取xml中node的值
    getNodeValue(fatherNode,attrName){
        if(!fatherNode){
            return;
        }

        let node = fatherNode.getElementsByTagName(attrName);
        if(!node && node.length > 0){
            return;
        }
        let nodeAttr = node[0];
        if(!nodeAttr){
            return;
        }

        let nodeValue = nodeAttr.textContent;
        return nodeValue;
    }

    //通过分割字符串获取坐标如果"0 0 0" -> {x:0,y:0,z:0}
    getCoordinate(strCoordinate){
        let strList = [];
        strList = strCoordinate.split(/\s+/);
        if(strList && strList.length >= 3){
            let point = {
                x:strList[0],
                y:strList[1],
                z:strList[2]
            }
            return point;
        }
    }
}

const electricalCableCommand = new ElectricalCableCommand();
export {electricalCableCommand};